`timescale 1ns/1ps
`default_nettype none

/* NOTE:
* - 对比度处理
* - 对比度系数2bit整数,6bit小数
*/

module contrast_adjust (
    input  wire         I_sclk,
    input  wire         I_rst_n,
    input  wire         I_contrast_en,
    input  wire [7:0]   I_contrast_coe,
    input  wire [7:0]   I_data_in,
    output wire [7:0]   O_data_out
);
//------------------------Parameter----------------------

//------------------------Local signal-------------------
wire [7:0]  contrast;
reg  [8:0]  data;
reg  [17:0] product;
reg  [11:0] sum;
reg  [7:0]  result;

//------------------------Instantiation------------------

//------------------------Body---------------------------
assign O_data_out = result;

assign contrast = (I_contrast_en)? I_contrast_coe : 8'd64;

always @(posedge I_sclk) begin
    data <= I_data_in - 8'd128;
end

always @(posedge I_sclk) begin
    product <= $signed(data) * $signed({1'b0, contrast});
end

always @(posedge I_sclk) begin
    sum <= product[17:6] + 12'd128;
end

always @(posedge I_sclk) begin
    if (sum[11])
        result <= 1'b0;
    else if (sum[10:8] != 1'b0)
        result <= 8'd255;
    else
        result <= sum[7:0];
end

endmodule

`default_nettype wire

// vim:set ts=4 sw=4 et fenc=utf-8 fdm=marker:
